<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />

			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索名称、生产商、责任人" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table">
				<template #slot-status="{ scope }">
					<el-button text type="primary" bg @click="changeStatus(scope.row)">
						{{ scope.row.status == 0 ? '启用' : '禁用' }}
					</el-button>
				</template>
				<template #slot-info="{ scope }">
					<el-button text type="success" bg @click="getInfo(scope.row)"> 查看 </el-button>
				</template>
			</cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="instrument-info" setup>
import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus';
import { useForm } from '@cool-vue/crud';
const Form = useForm();
const { service } = useCool();

// 选项
const options = reactive({
	status: [
		{ label: '停用', value: 0, type: 'danger' },
		{ label: '启用', value: 1, type: 'success' }
	]
});

const getInfo = row => {
	const res = service.instrument.info.info(row.id);
	console.log(JSON.stringify(res));

	Form.value?.open({
		title: '医疗器械详情',
		items: [
			{
				label: '名称',
				prop: 'name',
				component: { name: 'el-input', props: { clearable: true }, value: 's' },
				required: true
			},
			{
				label: '类型',
				prop: 'type',
				component: { name: 'el-input', props: { clearable: true } },
				required: true
			},
			{
				label: '规格',
				prop: 'specification',
				component: { name: 'el-input', props: { clearable: true } },
				required: true
			},
			{
				label: '生产商',
				prop: 'manufacturer',
				component: { name: 'el-input', props: { clearable: true } },
				required: true
			},
			{
				label: '有效期',
				prop: 'expirationDate',
				component: {
					name: 'el-date-picker',
					props: { type: 'date', valueFormat: 'YYYY-MM-DD' }
				},
				required: true
			},
			{
				label: '库存',
				prop: 'stock',
				hook: 'number',
				component: { name: 'el-input-number' },
				required: true
			},
			{
				label: '状态',
				prop: 'status',
				component: { name: 'el-radio-group', options: options.status },
				value: 0,
				required: true
			},
			{
				label: '备注',
				prop: 'remark',
				component: { name: 'el-input', props: { type: 'textarea', rows: 4 } }
			},
			{
				label: '责任人',
				prop: 'responsiblePerson',
				component: { name: 'el-input', props: { clearable: true } },
				required: true
			},
			{
				label: '批次号',
				prop: 'batchNumber',
				component: { name: 'el-input', props: { clearable: true } },
				required: true
			}
		]
	});
};
// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: '名称',
			prop: 'name',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '类型',
			prop: 'type',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '规格',
			prop: 'specification',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '生产商',
			prop: 'manufacturer',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '有效期',
			prop: 'expirationDate',
			component: {
				name: 'el-date-picker',
				props: { type: 'date', valueFormat: 'YYYY-MM-DD' }
			},
			required: true
		},
		{
			label: '库存',
			prop: 'stock',
			hook: 'number',
			component: { name: 'el-input-number' },
			required: true
		},
		{
			label: '状态',
			prop: 'status',
			component: { name: 'el-radio-group', options: options.status },
			value: 0,
			required: true
		},
		{
			label: '备注',
			prop: 'remark',
			component: { name: 'el-input', props: { type: 'textarea', rows: 4 } }
		},
		{
			label: '责任人',
			prop: 'responsiblePerson',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: '批次号',
			prop: 'batchNumber',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: '名称', prop: 'name', minWidth: 140 },
		{ label: '类型', prop: 'type', minWidth: 140 },
		{ label: '规格', prop: 'specification', minWidth: 140 },
		{ label: '生产商', prop: 'manufacturer', minWidth: 140 },
		{
			label: '有效期',
			prop: 'expirationDate',
			minWidth: 140,
			sortable: 'custom',
			component: { name: 'cl-date-text' }
		},
		{ label: '库存', prop: 'stock', minWidth: 140 },
		{ label: '状态', prop: 'status', dict: options.status, minWidth: 120 },
		{ label: '备注', prop: 'remark', showOverflowTooltip: true, minWidth: 200 },
		{ label: '责任人', prop: 'responsiblePerson', minWidth: 140 },
		{ label: '批次号', prop: 'batchNumber', minWidth: 140 },
		{
			label: '创建时间',
			prop: 'createTime',
			minWidth: 170,
			sortable: 'desc',
			component: { name: 'cl-date-text' }
		},
		{
			label: '更新时间',
			prop: 'updateTime',
			minWidth: 170,
			sortable: 'custom',
			component: { name: 'cl-date-text' }
		},
		{ width: 300, type: 'op', buttons: ['slot-info', 'slot-status', 'edit', 'delete'] }
	]
});
const changeStatus = row => {
	ElMessageBox.alert('确定修改器械状态吗', '提示', {
		// if you want to disable its autofocus
		// autofocus: false,
		showCancelButton: true,
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		callback: (action: Action) => {
			service.instrument.info
				.changeStatus({
					id: row.id,
					status: row.status == 0 ? 1 : 0
				})
				.then(() => {
					if (action == 'confirm') {
						ElMessage({
							type: 'success',
							message: `修改成功`
						});
						Crud.value?.refresh();
					}
				})
				.catch(err => {
					ElMessage({
						type: 'error',
						message: err
					});
				});
		}
	});
};
// cl-crud
const Crud = useCrud(
	{
		service: service.instrument.info
	},
	app => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
